import api from "@/servers/apiConfig";
import request from "@/servers/axiosConfig";

/**
 * 获取用户信息
 * @returns 
 */
function getInfo() {
  return request(api.user.info);
}

/**
 * 向邮箱发送验证码
 * @param {string} email 邮箱
 * @returns 
 */
function sendCode(email) {
  return request(api.user.code, { data: { email } });
}

/**
 * 登录
 * @param {string} email 
 * @param {string} code 
 * @returns 
 */
function login(email, code) {
  return request(api.user.login, { data: { email, code } });
}

/**
 * 注销登录
 * @returns 
 */
function logout() {
  return request(api.user.logout);
}

/**
 * 订阅项目
 * @param {Array<string>} ids 
 * @returns 
 */
function subscribe(ids) {
  return request(api.user.subscribe, { data: { subscribe_list: ids } });
}

/**
 * 更新用户的列表展示样式
 * @param {number} listDisplay - 0: 完整模式, 1: 简约模式
 */
function updateListDisplay(listDisplay) {
  return request(api.user.update, { data: { config: { list_display: listDisplay } } });
}

/**
 * 更新用户的窗口打开模式，是否打开新窗口
 * @param {boolean} openInNewWindow - true: 打开新窗口, false: 不打开新窗口
 */
function updateOpenInNewWindow(openInNewWindow) {
  return request(api.user.update, { data: { config: {open_in_new_window: openInNewWindow } } });
}

export const user = {
  getInfo,
  sendCode,
  login,
  logout,
  subscribe,
  updateListDisplay,
  updateOpenInNewWindow,
}